package com.cwt_taxi_xa.custom_view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.cwt_taxi_xa.R;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by undi on 2019/6/21 17:50
 */
public class AirConditionView extends View /*implements IViewAirCondition*/ {
    final String TAG = "AirConditionView";
    public int width = 0;
    public int height = 0;
    int paddingLeft = 7;
    int level = 2;
    int widthSize;  //图片
    int heightSize;
    Bitmap bitmap;
    List<Bitmap> windLevel = new ArrayList<>();

    public AirConditionView(Context context) {
        super(context);
        init();
    }

    public AirConditionView(Context context,AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AirConditionView(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_small_fan);
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_f_wind_level_1,options));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_f_wind_level_2,options));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_f_wind_level_3,options));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_f_wind_level_4,options));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_f_wind_level_5,options));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_f_wind_level_6,options));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_f_wind_level_7,options));

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        //setMeasuredDimension(widthMeasureSpec, height);
        int w = 0;
        int h = 0;

        Log.e(TAG,"onMeasure MinimumWidth:widthMeasureSpec " + getSuggestedMinimumWidth() + ":" + widthMeasureSpec + widthMeasureSpec + " MinimumHeight:heightMeasureSpec " + getSuggestedMinimumHeight() + ":" + heightMeasureSpec);
        w = Math.max(w, getSuggestedMinimumWidth());
        h = Math.max(h, getSuggestedMinimumHeight());

        widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);
        heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);

        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if(level > 0){
            int paddingTop = 0;
            int leftPadding = leftPadding();
            for(int i = 0; i< level; i++){
                //风量图片居中显示
                if(paddingTop == 0){
                    paddingTop = (heightSize - windLevel.get(i).getHeight())/4 - 5;//-5调整位置
                }
                canvas.drawBitmap(windLevel.get(i), (leftPadding + 5 ) + bitmap.getWidth() + i * paddingLeft, paddingTop, null);
            }
            canvas.drawBitmap(bitmap,leftPadding,paddingTop +15,null);
        }
    }

    private int leftPadding(){
        return heightSize - (bitmap.getWidth() + windLevel.size() * paddingLeft + windLevel.size() * windLevel.get(0).getWidth());
    }

    public void selectLevel(int value){
        this.level = value;
        invalidate();
    }

}
